<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="220px">
        <div class="sidebar">
          <div class="logo-container">
            <img src="@/assets/logo.png" alt="Logo" class="logo">
            <h3>社团联合会</h3>
          </div>
          
          <el-menu
            default-active="/association/dashboard"
            class="sidebar-menu"
            background-color="#001529"
            text-color="#fff"
            active-text-color="#409EFF"
            router>
            
            <el-menu-item index="/association/dashboard">
              <i class="el-icon-s-home"></i>
              <span>控制台概览</span>
            </el-menu-item>
            
            <el-submenu index="club">
              <template slot="title">
                <i class="el-icon-s-cooperation"></i>
                <span>社团管理</span>
              </template>
              <el-menu-item index="/association/club-approval">社团审核</el-menu-item>
              <el-menu-item index="/association/club-list">社团列表</el-menu-item>
            </el-submenu>
            
            <el-submenu index="activity">
              <template slot="title">
                <i class="el-icon-s-flag"></i>
                <span>活动管理</span>
              </template>
              <el-menu-item index="/association/activity-approval">活动审核</el-menu-item>
              <el-menu-item index="/association/activity-list">活动列表</el-menu-item>
            </el-submenu>
            
            <el-menu-item index="/association/notice">
              <i class="el-icon-bell"></i>
              <span>系统通知</span>
            </el-menu-item>

            <el-menu-item index="/association/finance">
              <i class="el-icon-money"></i>
              <span>财务管理</span>
            </el-menu-item>
             <el-menu-item index="/association/request">
              <i class="el-icon-money"></i>
              <span>资金申请</span>
            </el-menu-item>
            
          </el-menu>
        </div>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header height="60px">
          <div class="header-left">
            <i class="el-icon-s-fold toggle-sidebar"></i>
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/association/dashboard' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ $route.meta.title }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="header-right">
            <el-dropdown trigger="click">
              <span class="user-dropdown">
                <el-avatar size="small" :src="adminInfo.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"></el-avatar>
                <span class="username">{{ adminInfo.nickname || adminInfo.username || '管理员' }}</span>
                <i class="el-icon-arrow-down"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided @click.native="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 内容区 - 渲染子路由组件 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import httpInstance from '@/utils/http';
export default {
  name: 'AdminLayout',
  data() {
    return {
      adminInfo: JSON.parse(localStorage.getItem('adminInfo') || '{}')
    }
  },
  methods: {
    getUserInfo(){
        httpInstance({
          url:'/api/user/info',
          method:'post'
        }).then(res => {
          console.log("res", res)
          this.adminInfo = res.data;
        }).catch(err => {
          this.$message.error('获取用户信息失败');
        })
      },
    // 退出登录
    handleLogout() {
      this.$confirm('确定要退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清除管理员token和信息
        localStorage.removeItem('token');
        localStorage.removeItem('userType');
        localStorage.removeItem('role');
        localStorage.removeItem('adminInfo');
        
        // 跳转到管理员登录页
        this.$router.push('/login');
        this.$message.success('已退出登录');
      }).catch(() => {
        // 取消操作
      });
    }
  },
  created() {
    // 检查token是否存在
    const token = localStorage.getItem('token')
    if (!token) {
      this.$router.push('/login');
      return;
    }
    
    // 如果当前路径是/association，重定向到控制台
    if (this.$route.path === '/association') {
      this.$router.push('/association/dashboard');
    }
    
    this.getUserInfo();
  }
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  overflow: hidden;
}

/* 侧边栏样式 */
.sidebar {
  height: 100%;
  background-color: #001529;
  color: white;
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  background-color: #002140;
}

.logo {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

/* 顶部导航样式 */
.el-header {
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.toggle-sidebar {
  font-size: 20px;
  margin-right: 20px;
  cursor: pointer;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 8px;
}

/* 主内容区样式 */
.el-main {
  background-color: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

/* 确保容器可以滚动 */
.el-container {
  height: 100vh;
  overflow: hidden;
}

.el-container .el-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
</style>
